require("../../assets/css/basic.less");
require("../../assets/css/normalize.css");
require("../../assets/css/property.css");
require("../../assets/css/reset.css");
require("../../assets/fonts/font-icon2/iconfont.css");
const axios = require("axios");
require("../login/login.less");

document.addEventListener("DOMContentLoaded", function () {
	const eleObj = {};
	eleObj.phonenumber = document.querySelector(" #phonenumber ");
	eleObj.password = document.querySelector(" #password ");
	eleObj.submitBtn = document.querySelector(" #submitBtn ");
	eleObj.errMsg = document.querySelector(" .text-errMsg ");
	eleObj.submitBtn.addEventListener("click", function () {
		// 条件正则表达式
		const regExp = [
			/^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/, //手机号验证
			/^[a-zA-Z]\w{5,11}$/, //密码验证
		];
		const phonenumber = eleObj.phonenumber.value;
		const password = eleObj.password.value;
		//判断前端值是否正确
		if (!phonenumber || !regExp[0].test(phonenumber)) {
			eleObj.errMsg.textContent = "手机号格式错误，请重新输入！！";
		} else if (!password || !regExp[1].test(password)) {
			eleObj.errMsg.textContent = "密码格式错误,必须以字母开头,长度6-12位";
		} else {
			eleObj.errMsg.textContent = "";
			//发送异步请求
			// console.log( 12 )
			axios
				.post(" http://fitness.h5.itsource.cn:3701/api/user/login", {
					account: phonenumber,
					password: password,
				})
				.then(function (res) {
					//请求成功处理
					// console.log(res.data);
					//登陆成功处理
					if (res.data.errno === 0) {
						localStorage.setItem("token", res.data.data.token); //将token存储起来，以备后用
						location.href = "./index.html";
						alert("登陆成功");
					} else {
						//登录失败处理
						eleObj.errMsg.textContent = res.data.msg;
					}
				});
		}
	});
});
